package com.example.startscreen;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.content.res.AssetManager;
import android.util.Log;
import android.util.Xml;

public class LocDescriptionFileParser {
	static final String DIVACT = "divact";
	static final String LOCATION = "location";
	static final String NAME_MNE = "name_mne";
	static final String TEXT_MNE = "text_mne";
	static final String NAME_ENG = "name_eng";
	static final String TEXT_ENG = "text_eng";

	public List<LocDescriptionFile> readLocation(AssetManager asset, String locationXmlFile) {
		List<LocDescriptionFile> locations = null;
		XmlPullParser parser = Xml.newPullParser();
		try {
			//InputStream in = new FileInputStream(locationXmlFile);
			InputStream in = asset.open(locationXmlFile);
			// auto-detect the encoding from the stream
			parser.setInput(in, null);

			
			// Read the XML document
			int eventType = parser.getEventType();
			LocDescriptionFile location = null;
			boolean done = false;
			while (eventType != XmlPullParser.END_DOCUMENT && !done){
				String name = null;
				switch (eventType){
					case XmlPullParser.START_DOCUMENT:
						locations = new ArrayList<LocDescriptionFile>();
						//Log.i("LocDescriptionFileParser", "StartDocumentTag");
						break;
					case XmlPullParser.START_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase(LOCATION)){
							location = new LocDescriptionFile();
							//Log.i("LocDescriptionFileParser", "Location tag");
						} else if (location != null){
							if (name.equalsIgnoreCase(NAME_MNE)){
								location.setName_mne(parser.nextText());
								//Log.i("LocDescriptionFileParser", "Name_MNE: " + location.getName_mne());
							} else if (name.equalsIgnoreCase(TEXT_MNE)){
								location.setText_mne(parser.nextText());
								//Log.i("LocDescriptionFileParser", "Text_MNE: " + location.getText_mne());
							} else if (name.equalsIgnoreCase(NAME_ENG)){
								location.setName_eng(parser.nextText());
								//Log.i("LocDescriptionFileParser", "Name_ENG: " + location.getName_eng());
							} else if (name.equalsIgnoreCase(TEXT_ENG)){
								location.setText_eng(parser.nextText());
								Log.i("LocDescriptionFileParser", "Text_ENG: " + location.getText_eng());
							}	
						}
						break;
					case XmlPullParser.END_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase(LOCATION) && location != null){
							locations.add(location);
						} else if (name.equalsIgnoreCase(DIVACT)){
							done = true;
						}
						break;
				}
				eventType = parser.next();
			}
		} catch (Exception e) {
			Log.e("AndroidNews::PullFeedParser", e.getMessage(), e);
			e.printStackTrace();
		}
		return locations;
	}

}
